﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;  
using System.Data;
using System.Windows.Forms;


namespace NNLInterface
{
     
    class ExcelImport
    {
        /// <summary>   
        /// 读取Excel文档   
        /// </summary>   
        /// <param name="Path">文件名称</param>   
        /// <returns>返回一个数据集</returns>   
        public static DataTable ExcelToDS(string excelFile)
        {
            try
            {
                //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;";
                //此处需要加上HDR=yes和IMEX=1指定第一行为表头
                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                string strExcel = "";
                OleDbDataAdapter myCommand = null;
                DataSet ds = null;
                strExcel = "select * from [sheet1$]";
                myCommand = new OleDbDataAdapter(strExcel, strConn);
                ds = new DataSet();
                myCommand.Fill(ds, "table1");
                DataTable dt = ds.Tables[0];
                return ds.Tables[0];
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return null;
            }
          
        }

    }
}
